/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import path from 'path';

import { expect } from '@jest/globals';


import { runHvigorAsync} from '../../common/common';
import { addOhPackageDps, commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [ModuleTemplateType.StaticLibrary],
};

/**
 * 1、创建stage工程，添加staticLibrary模块
 * 2、entry依赖staticLibrary
 * 3、staticLibrary模块中build-profile.json5配置branchElimination = true
 * 4、构建har
 * 5、查看是否构建成功
 * 6、修改har目录名后clean，再构建hap
 * 7、重复步骤5
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const entryRootPath = project.getModule('entry').moduleRootPath();
  const harRootPath = project.getModule('staticLibrary').moduleRootPath();

  // entry依赖staticLibrary
  await addOhPackageDps(entryRootPath, { 'staticLibrary': 'file:../staticLibrary' });

  // staticLibrary中配置branchElimination = true
  await commonJson5Operation(path.resolve(harRootPath, 'build-profile.json5'), async (json5: any) => {
    json5.buildOption = {
      'arkOptions': {
        'branchElimination': true
      }
    };
    json5.buildOptionSet = [
      {
        'name': 'release',
        'arkOptions': {
          'obfuscation': {
            'ruleOptions': {
              'enable': false,
              'files': [
                './obfuscation-rules.txt'
              ]
            },
            'consumerFiles': [
              './consumer-rules.txt'
            ]
          },
          'branchElimination': true
        }
      }
    ];
    json5.targets = [
      {
        'name': 'default',
        'config': {
          'buildOption': {
            'arkOptions': {
              'branchElimination': true
            }
          }
        }
      },
      {
        'name': 'ohosTest'
      }
    ];
  });

  // 工程级的build-profile.json5修改
  await commonJson5Operation(path.resolve(projectRoot, 'build-profile.json5'), async (json5: any) => {
    const product = json5.app.products.find((item: any) => item.name === 'default');
    product.buildOption = {
      'strictMode': {
        'caseSensitiveCheck': true,
        'useNormalizedOHMUrl': true
      },
      'arkOptions': {
        'branchElimination': true
      }
    };
    json5.app.buildModeSet = [
      {
        'name': 'debug',
      },
      {
        'name': 'release',
        'buildOption': {
          'arkOptions': {
            'branchElimination': true
          }
        }
      }
    ];
  });

  // 构建har
  const commandsHarRelease = '--mode module -p product=default -p module=staticLibrary@default -p buildMode=release assembleHar'.split(' ');
  const result1 = await runHvigorAsync(projectRoot, commandsHarRelease);
  record.addStep('修改har的build-profile的buildOption中的branchElimination', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');


};

TestCaseBuilder.builder()
  .caseName('AR20241114745641')
  .description('分支消除配置开关')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.AR).build())
  .testFun(testfun)
  .load();